package mainpackage;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: root
 * Date: 24.09.2009
 * Time: 0:12:41
 * To change this template use File | Settings | File Templates.
 */
public class SoundAccompaniment implements Runnable{

    ObservableHashMap<String, String> currentMap;
    private static final int EXTERNAL_BUFFER_SIZE = 128000;
    int	nBytesRead;

    String prefix;
    byte[]	abData;
    File soundFile;
    AudioInputStream audioInputStream = null;
    AudioFormat audioFormat;
    SourceDataLine line;
	DataLine.Info	info;
    String soundFiles[];

    public SoundAccompaniment(ObservableHashMap<String, String> currentMap, String pathWav) throws LineUnavailableException, UnsupportedAudioFileException, IOException {
        this.currentMap = currentMap;
        this.currentMap.addMonitor(this);

        soundFiles = new String[4];
        prefix = pathWav;
        soundFiles[0] = prefix + "_client.wav";
        soundFiles[2] = prefix + "_window.wav";
        soundFile = new File(prefix + "_13.wav");
        audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        audioFormat = audioInputStream.getFormat();
		info = new DataLine.Info(SourceDataLine.class,
												 audioFormat);
        line = (SourceDataLine) AudioSystem.getLine(info);
	    line.open(audioFormat);
    }

    public synchronized  void run() {
        try{
            while(true){
                wait();
                String temp = getLastKey();
                try {
                    play(temp, currentMap.get(temp).toString());
                } catch (LineUnavailableException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                catch (UnsupportedAudioFileException ue) {
                    ue.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        } catch(InterruptedException ie){
            ie.printStackTrace();
        }
    }

    public  void play(String client, String window) throws UnsupportedAudioFileException,LineUnavailableException {
        int i = 0;
        soundFiles[1] = prefix + "_" + client + ".wav";
        soundFiles[3] = prefix + "_" + window + ".wav";
        try{
            for (i=0; i<4; i++){
                soundFile = new File(soundFiles[i]);
                audioInputStream = AudioSystem.getAudioInputStream(soundFile);
                line.start();
		        readByte();
            }
        } catch(IOException ie){
            System.out.println(soundFiles[i] + "file not found");
        }
        //line.close();
    }

    public void readByte()
    {
        nBytesRead = 0;
		abData = new byte[EXTERNAL_BUFFER_SIZE];
		while (nBytesRead != -1)
		{
			try
			{
				nBytesRead = audioInputStream.read(abData, 0, abData.length);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			if (nBytesRead >= 0)
			{
				line.write(abData, 0, nBytesRead);
			}
		}

        line.drain();
    }

     public String getLastKey(){
         String resultString="";
        Iterator iter =  currentMap.keySet().iterator();
         while(iter.hasNext()) {
             resultString = iter.next().toString();
         }
        return resultString;
    }
}
